// #ifdef H5
import jWeixin from '@/js_sdk/jweixin'
import { showError } from '@/core/app'
import { isWeixinOfficial } from '@/core/platform'
import wxofficialApi from '@/api/wxofficial'
import WxofficialSettingModel from '@/common/model/wxofficial/Setting'

/**
 * 注入微信jssdk配置信息
 */
const setConfig = async () => {
  // 获取当前页面url (不包含#及其后面部分)
  const url = window.location.href.split('#')[0]
  // 请求后端获取jssdkConfig
  const response = await wxofficialApi.jssdkConfig(url)
  // 注入配置信息
  jWeixin.config(response.data.config)
  // 提示错误
  jWeixin.error(res => {
    console.error('jWeixin.error', res)
    showError(`微信链接分享配置错误：${res.errMsg}`)
  })
}

/**
 * 设置分享卡片的内容
 */
const updateShareData = async (shareData) => {
  // 是否开启微信公众号链接分享卡片
  const enabled = await WxofficialSettingModel.isWxofficialLinkShareCard()
  if (isWeixinOfficial || !enabled) {
    return
  }
  // 整理分享数据
  // const shareData = { title, desc, link, imgUrl }
  shareData.link = shareData.link || window.location.href
  jWeixin.ready(() => {
    console.log('jWeixin.ready', shareData)
    jWeixin.updateAppMessageShareData(shareData)
    jWeixin.updateTimelineShareData(shareData)
    // jWeixin.onMenuShareAppMessage(shareData)
    // jWeixin.onMenuShareTimeline(shareData)
  })
}

export default {
  setConfig,
  updateShareData
}
// #endif
